/*
 * Listeners:
 * postAddNew - after a new item is inserted
 * postAdd - after an item is inserted (may not be a new one)
 */
ria.buildList = function(spec){
    //spec.ds
    //spec.containerElement
    //spec.item
    //spec.tpls
    //spec.decorators
    //spec.defaultTpl
    //spec.listInstanceName
    //spec.extra
    if(!spec.extra) {
        spec.extra = {};
    }
    spec.items = [];
    var ret = {
        add : function (json) {
            if (json.__newItem != undefined) {
                json.__newItem = json.__newItem;
            }
            else {
                json.__newItem = true;
            }
            var index = spec.items.length;
            var itemDomId = 'listItem' + index;
            if(spec.listItemDecorator) {
                spec.containerElement.append(spec.listItemDecorator.replace('${_itemDomId}',itemDomId));
            }
            else {
                spec.containerElement.append("<div id='" + itemDomId + "'></div>");
            }
            console.log('uai cacilda!');
            var itemSpec = this.itemSpec(json.ds, $('#' + itemDomId), clone(spec.extra));
            itemSpec.extra["_jsObjName"] = spec.listInstanceName + '.getItem(' + index + ')';
            itemSpec.extra["_list"] = this;
            itemSpec.extra["_listIndex"] = index;
            itemSpec.extra["_itemDomId"] = itemDomId;
            spec.items[index] = this.buildTplItem(itemSpec);
            if(json.__newItem) {
                if( this.postAddNew ) { this.postAddNew({item:spec.items[index]}) };
            }
            this.postAdd({item:spec.items[index]});
        },
        postAdd : function(json) {
            json.item.display();
        },
        itemSpec : function(itemDs, element, extra) {
            return {
                ds : itemDs,
                element : element,
                tpls : spec.tpls,
                decorators : spec.decorators,
                extra : extra,
                actualTpl : spec.actualTpl,
                commands : ( spec.extra.itemCommands != undefined ? clone(spec.extra.itemCommands) : {})
            };
        },
        render : function () {
            for(var i in spec.ds) {
                this.add({ds:spec.ds[i],__newItem:false});
            }
            return this;
        },
        getItem : function(index) {return spec.items[index];},
        removeItem : function(index) { // remove da tela e do ds
            this.getItem(index).getElement().remove();
            //spec.items.splice(index,1);
            delete spec.items[index];
        },
        removeItemById : function(id) { // busca o ds com o id, e chama o removeItem
            for(i in spec.items) {
                if(spec.items[i].getDs().id == id) {
                    this.removeItem(i);
                }
            }
        },
        buildTplItem : function(spec) {  // constroi cada item
            return ria.buildItem(spec);
        }
    };
    for(i in spec.extra.commands) {
        ret[i] = function() {
            var f = spec.extra.commands[i];
            var s = spec;
            return function(json) {
                //console.log(f);
                //console.log(s);
                f.apply(this,[s,json]);
            }
            //.apply(this);
        }();
    }
    return ret;
};